import { createFilter } from "@rollup/pluginutils";
export default function replace(options = {}) {
    const filter = createFilter(options.include, options.exclude);
    const env = options.env || "browser";

    const reg = /\/\/ifdef\s+(\w+)\s*\n([\s\S]*?)\n\s*\/\/endif\s*/gi;
    return {
        name: "rollup-plugin-conditional",

        transform(code, id) {
            if (!filter(id)) return null;
            const conditionedCode = code.replace(reg, (all, p1, p2) => {
                console.log(p1);
                return p1.toLowerCase() === env.toLowerCase() ? p2 : "";
            });
            const result = {
                code: conditionedCode,
            };
            if (options.sourceMap !== false && options.sourcemap !== false)
                result.map = { mapping: "" };

            return result;
        },
    };
}
